package com.xuzhijie.pms.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xuzhijie.pms.common.Constants;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.Property;
import com.xuzhijie.pms.entity.Renovation;
import com.xuzhijie.pms.entity.vo.RenovationVo;
import com.xuzhijie.pms.service.IPropertyService;
import com.xuzhijie.pms.service.IRenovationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-17
 */
@RestController
@RequestMapping("/renovation")
public class RenovationController {

    @Autowired
    private IPropertyService propertyService;

    @Autowired
    private IRenovationService service;

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer page,
                           @RequestParam Integer size,
                           @RequestParam Integer currentProgress,
                           @RequestParam Integer isReturn) {

        return service.findPage(page, size,currentProgress,isReturn);
    }

    @GetMapping("/wx-getRenoByOwner")
    public Result getRenoByOwner(@RequestParam String id) {
        QueryWrapper<Renovation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applicant",id);
        List<Renovation> list = service.list(queryWrapper);
        Map<String,String> map = new HashMap<>();
        for (Renovation renovation : list) {
            RenovationVo renovationVo = new RenovationVo(renovation);
            BeanUtil.copyProperties(renovation,renovationVo);
            Property one = propertyService.getById(renovation.getBuilding());
            map.put(one.getDetail(),renovationVo.getRenovationCurrentProgress());
        }


        return Result.success(map);
    }

    @PostMapping("/wx-createSendReno")
    public Result createSendReno(@RequestBody Map<String, Object> request) {
        Object renos = request.get("renos");
        String str = renos.toString();
        JSONArray jsonArray = new JSONArray(str);

        String[] arr = jsonArray.toArray(new String[0]);
        return service.createSendReno(arr,request.get("id").toString());
    }
}

